package com.keat.qqclient.service;

import com.keat.qqcommon.Message;
import com.keat.qqcommon.MessageType;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import com.keat.qqclient.ui.UserClientController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.application.Platform;

import javax.print.DocFlavor;

/**
 * @author Keat-Jie
 * @version 1.0
 */
public class ClientConnectServerThread extends Thread{
    private Socket socket;
    private UserClientController uiObj;
    public String records;

    public static String getByDate(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public ClientConnectServerThread(Socket socket, UserClientController uiObj) {
        this.socket = socket;
        this.uiObj = uiObj;
    }

    @Override
    public void run() {
        //uiObj.list_user.setItems(data);
        while (true) {//Thread需要在后台和服务器通信
            try {
                System.out.println("客户端线程,等待读取从服务器端发送的消息");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();//如果没有发送,阻塞


                    if (message.getMesType().equals(MessageType.MESSAGE_RET_ONLINE_FRIEND)) {
                        String[] onlineUsers = message.getContent().split(" ");
                        System.out.println("\n=====当前在线用户列表=====");
                        Platform.runLater(() -> {
                            uiObj.list_user.getItems().clear();
                            for (int i = 0; i < onlineUsers.length; i++) {
                                System.out.println("用户: " + onlineUsers[i]);
                                uiObj.list_user.getItems().add(onlineUsers[i]);
                                if(!uiObj.chatdata.containsKey(onlineUsers[i])) {
                                    uiObj.chatdata.put(onlineUsers[i], "");
                                }
                            }
                        });
                    } else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                        records = uiObj.chatdata.get(message.getSender());
                        records += "\n\n[" + getByDate() + "] " + message.getSender() + ":\n" + message.getContent();
                        uiObj.chatdata.put(message.getSender(), records);
                        Platform.runLater(() -> {
                                    uiObj.text_record.clear();
                                    uiObj.text_record.setText(records);
                                });
                        System.out.println("\n" + message.getSender()
                                + " 对 " + message.getGetter() + " 说: " + message.getContent());
                    } else if (message.getMesType().equals(MessageType.MESSAGE_TO_ALL_MES)) {
                        records = uiObj.chatdata.get(message.getSender());
                        records += "\n\n[" + getByDate() + "] [广播] " + message.getSender() + ":\n" + message.getContent();
                        uiObj.chatdata.put(message.getSender(), records);
                        Platform.runLater(() -> {
                            uiObj.text_record.clear();
                            uiObj.text_record.setText(records);
                        });
                        System.out.println(records);
                    } else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)) {
                        System.out.println("\n" + message.getSender() + " 给 " + message.getGetter()
                                + " 发送文件: " + message.getSrc() + " 到我的电脑的目录 " + message.getDest());
                        FileOutputStream fileOutputStream = new FileOutputStream(message.getDest());
                        fileOutputStream.write(message.getFileBytes());
                        fileOutputStream.close();
                        System.out.println("\n 保存文件成功");
                        records = uiObj.chatdata.get(message.getSender());
                        records += "\n\n[" + getByDate() + "] " + message.getSender() + ":\n[传输文件] " + message.getDest();
                        Platform.runLater(() -> {
                            uiObj.text_record.setText(records);
                        });
                    } else {
                        System.out.println("其他类型的message");

                    }

                } catch(Exception e){
                    e.printStackTrace();
                }



        }
    }

    public Socket getSocket() {
        return socket;
    }


}
